
function WebTable() {
	this.ID   = '';
	this.Head = ['学号', '姓名', '成绩'];
}

function removeAllSpaces(str) {
    return str.replace(/\s+/g, '');
}

WebTable.prototype.loadScore = function (){
	
	this.getTableId();
	if(typeof this.ID == 'undefined' || this.ID == '')
		throw 'NULL WEB';
	
	// alert(this.ID)
    var trList = $('#' + this.ID).find('tr');
	
	// 定位 '学号', '姓名', '成绩' 所在的列
    var tdList = trList.eq(0).find('td');
	var content;
	for (var i = 0; i < tdList.length; i++) {
		content = $.trim(tdList.eq(i).text());
		//k = tdList.eq(1).val();
		for (var j = 0; j < this.Head.length; j++) {
			if(content == this.Head[j]){
				this.Head[j] = i;
				break;
			}
		}
	}
	
	// 提取所有学生的'学号', '姓名', '成绩'的具体信息
	var Data = new Array(trList.length-1);
    for (var i = 1; i < trList.length; i++) {
		tdList = trList.eq(i).find('td');
		var score = tdList.eq(this.Head[2]).find('input');
        Data[i-1] = [
		$.trim(tdList.eq(this.Head[0]).text()), $.trim(tdList.eq(this.Head[1]).text()), score
		];
    }
	return new WebScore(Data);
}

WebTable.prototype.getTableId = function (){
	var	content, counter;
	var HeadLen = this.Head.length;
	
	// 基于jQuery查找页面中所有的Table
	var tableList = $('table');
	// 在每个Table的每一行每一列中查找同时含有“学号”、“姓名”和“成绩”的行
	for(var t = 0; t < tableList.length; t++){
		var trList = $(tableList[t]).find('tr');
		var tdList = trList.eq(0).find('td');
		counter = 0;
		for(var i = 0; i < tdList.length; i++){
			td = tdList.eq(i);
			content = removeAllSpaces(td.text());
			
			for (var j = 0; j < HeadLen; j++) {
				if(content == this.Head[j]){
					counter++;
					//一旦找到同时含有“学号”、“姓名”和“成绩”的Table，则在DOM树种回溯其父DOM节点
					if(counter == HeadLen){
						var a = td, b = $(td.html());
						// 一旦发现其父DOM节点是Table类型，则停止回溯
						while(!b.is('table')){
							a = a.parent();
							b = $(a.html());
						}
						this.ID = $(b).attr('id');
						return;
					}
				}
			}
		}
	}
}
